from xml.sax import make_parser, handler
import math
import os, sys, random, bisect, datetime, subprocess
import fileinput
import shutil

class Vehicle:
    def __init__(self, vid, pos, speed):
        self._vid = vid
        self._pos = pos
        self._speed = speed
        
    def getSpeed(self):
        return self._speed
    
    def getPos(self):
        return self._pos
    
    def getID(self):
        return self._vid
#end class Vehicle

class Road:
    def __init__(self, eid):
        self._eid = eid
        self._vehicles = {}
        
    def getVehicles(self):
        return self._vehicles
    
    def getID(self):
        return self._eid
    
    def addVehicle(self, vid, val):
        self._vehicles[vid] = val;
        
    def  __getitem__(self, key):
        return self._vehicles[key]
    
    def dump(self):
        for v in self._vehicles.keys():
            ostr = "vehicle : id = " + self._vehicles[v].getID()
            ostr = ostr + " pos = " + str(self._vehicles[v].getPos()) + " spd = " + str(self._vehicles[v].getSpeed())
            print ostr
#end class Road

class Snapshot:
    def __init__(self, time):
        self._time = int(time)
        self._edges = dict()
        
    def getTime(self):
        return self._time
    
    def hasRoad(self, eid):
        return self._edges.has_key(eid)
    
    def addRoad(self, eid, edge):
        self._edges[eid] = edge
        
    def getRoad(self, key):
        return self._edges[key]
    
    def getEdges(self):
        return self._edges
    
    def __setitem__(self, key, value):
        self.addRoad(key, value)
        
    def  __getitem__(self, key):
        return self._edges[key]
    
    def dump(self):
        for r in self._edges:
            print "road " + r
            self._edges[r].dump()
#end class Snapshot


class TraceReader(handler.ContentHandler):
    """Reads the dump file"""
    def __init__(self):
        self._snap = dict()
        self.__curTm = 0
        self.__curEdge = "invalid"
    #end __init__
    
    def getSnapshots(self):
        return self._snap
    
    def dump(self):
        for k in self._snap.keys():
            self._snap[k].dump()

    def startElement(self, name, attrs):
        if name == "timestep":
            tm = int(float(attrs["time"]))
            self._snap[tm] = Snapshot(tm)
            self.__curTm = tm;
        elif name == "edge":
            eid = attrs["id"];
            self._snap[self.__curTm].addRoad(eid, Road(eid))
            self.__curEdge = eid
        elif name == "vehicle":
            vid = attrs["id"]
            vpos = float(attrs["pos"])
            vspd = float(attrs["speed"])
            self._snap[self.__curTm].getRoad(self.__curEdge).addVehicle(vid, Vehicle(vid, vpos, vspd))
        #end if
    #end startElement
#end class

def readTrace(dumpf):
    reader = TraceReader()
    parser = make_parser()
    parser.setContentHandler(reader)
    parser.parse(dumpf)
    return reader



